

cap log close
log using "$log/16_02_7stack_summary_table_${S_DATE}", text replace


*-----------------------------------
*Compute the bias-corrected slopes
*-----------------------------------

use $temp/firmfe_value_split_all7stack, clear

merge m:1 betnr using "$temp/wz.dta"

drop if _merge == 2
drop _merge

gen rent_bc = .
gen cd_bc   = .
qui encode sector, g(sector_num)

local i_list = "20017 20087 20157"
dis "`i_list'"

foreach i in `i_list' {

dis as text ""
dis as text ""
dis as text "Window = `i'"

*Regression of fixed effects on values 
reghdfe std_fe std_V [aweight = av_worker_years] if window == `i', vce(cl betnr)
local slope_ols = _b[std_V]

*Split-sample IV regression using versioned data (2 ways)
ivregress 2sls std_fe (std_V1=std_V2) [aweight = av_worker_years] if window == `i', vce(cl betnr)

preserve
	keep if e(sample) == 1
	keep betnr firm_fe1 firm_fe2
	duplicates drop
	save "$temp/temp_betnr_`i'", replace
restore


*------------------------------------------------------------
* Compute bias-corrected R-sq
* R-sq-bc := (Cov(psi_1,V_1))^2/[Cov(psi_1,psi_2)*Cov(V_1,V_2)]
* numerator can also be computed as Cov(psi_2,V_2) or
* Cov(psi,V). Do all three below. 
*------------------------------------------------------------

*Compute covariances
qui{
corr std_fe1 std_V1 [aweight = av_worker_years] if window == `i', covariance
local num2 = r(cov_12)
corr std_fe1 std_fe2 [aweight = av_worker_years] if window == `i', covariance
local cov_fe = r(cov_12)
corr std_V1 std_V2 [aweight = av_worker_years] if window == `i', covariance
local cov_v = r(cov_12)
}

*Construct R-sq-bc
local rsq_bc2 = (`num2')^2/(`cov_fe'*`cov_v')

dis as text ""
di "Bias-corrected R-sq estimates for window `i':"
di `rsq_bc2'

dis as text ""
dis as text ""

*------------------------------------------------------------------------------- 
* Create bias-corrected estimates for rents and cds
*------------------------------------------------------------------------------- 

*Linear version (baseline)
qui{ 
ivregress 2sls firm_fe1 (value1=value2) if window==`i' [aweight = av_worker_years]
replace rent_bc = _b[value1]*value + _b[_cons] if window == `i'
replace cd_bc   = firm_fe - rent_bc if window == `i'
}

*-------------------------------------------------------------------------------
* Decompose variance in CDs explained by observables
*-------------------------------------------------------------------------------

*by betnr and window: merge on state, kreise, sector, industry
*use modal values for the observables in BHP for each betnr
*then:

reghdfe cd_bc [aweight = av_worker_years] if window==`i', a(bula)
reghdfe cd_bc [aweight = av_worker_years] if window==`i', a(kreis)
reghdfe cd_bc [aweight = av_worker_years] if window==`i', a(sector_num)
reghdfe cd_bc [aweight = av_worker_years] if window==`i', a(ind)
reghdfe cd_bc [aweight = av_worker_years] if window==`i', a(ind#kreis)

dis as text ""
dis as text ""
}

cap log close
clear

